/*
 Xern Entity Relocation Network
 Copyright (C) 2011 Ronie Salgado <roniesalg@gmail.com>

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/

#ifndef MESSAGETEMPLATE_HPP_INCLUDED
#define MESSAGETEMPLATE_HPP_INCLUDED

#include <string>
#include <map>
#include <vector>
#include <iostream>
#include <boost/shared_ptr.hpp>

namespace XernMetaverse
{

    // This file contains classes used to read message_template.msg, which
    // contains a description of the second life protocol. (Structure of the messages).
    class MessageVariableTemplate
    {
    public:
        MessageVariableTemplate();

        void BuildCache();

        size_t GetFixedSize() const;
        bool HasVariableSize() const;

        enum Type
        {
            VT_NULL = 0,
            VT_FIXED,
            VT_VARIABLE,
            VT_U8,
            VT_U16,
            VT_U32,
            VT_U64,
            VT_S8,
            VT_S16,
            VT_S32,
            VT_S64,
            VT_F32,
            VT_F64,
            VT_LLVECTOR3,
            VT_LLVECTOR3D,
            VT_LLVECTOR4,
            VT_LLQUATERNION,
            VT_LLUUID,
            VT_BOOL,
            VT_IPADDR, //< IPv4 address, 4 bytes
            VT_IPPORT,
            VT_U16VEC3,
            VT_U16QUAT,
            VT_S16ARRAY,
        };

        std::string name;
        Type type;
        unsigned int size;
    };

    class MessageBlockTemplate
    {
    public:
        MessageBlockTemplate();

        void BuildCache();

        size_t GetBlockSize() const;
        bool HasVariableSize() const;

        enum Quantity
        {
            BQ_SINGLE=0,
            BQ_MULTIPLE,
            BQ_VARIABLE
        };

        std::string name;
        Quantity quantityType;
        unsigned int numrepetitions;

        typedef std::vector<MessageVariableTemplate> VariablesType;
        VariablesType variables;

        typedef std::map<std::string, int> VariablesCacheType;
        VariablesCacheType variablesCache;

    private:
        int sizeCache;
        bool hasVariableSizeCache;
    };

    class MessageTemplate
    {
    public:
        MessageTemplate();

        void BuildCache();

        size_t GetMessageSize() const;
        bool HasVariableSize() const;

        enum Frequency
        {
            MF_FIXED = 0,
            MF_LOW,
            MF_MEDIUM,
            MF_HIGH,
        };

        enum Encoding
        {
            ME_UNENCODED = 0,
            ME_ZEROCODED,
        };

        std::string name;
        Frequency frequency;
        unsigned int messageId;
        bool isTrusted;
        Encoding encoding;

        typedef std::vector<boost::shared_ptr<MessageBlockTemplate> > BlocksType;
        BlocksType blocks;

        typedef std::map<std::string, int> BlocksCacheType;
        BlocksCacheType blocksCache;

    private:
        int sizeCache;
        bool hasVariableSizeCache;
    };

    class MessageTemplates
    {
    public:
        MessageTemplates();

        void BuildCache();

        unsigned int versionMajor, versionMinor;
        unsigned int crc32;

        typedef std::vector<boost::shared_ptr<MessageTemplate> > TemplatesType;
        TemplatesType templates;

        typedef std::map<std::string, int> TemplatesCacheType;
        TemplatesCacheType templatesCache;

        typedef std::pair<MessageTemplate::Frequency, unsigned int> MessageId;
        typedef std::map<MessageId, int> TemplatesIdsType;
        TemplatesIdsType templatesIds;
    };

    bool ParseMessageTemplates(const std::string &data, MessageTemplates &dest);
    bool ParseMessageTemplates(std::istream &in, MessageTemplates &dest);
};

#endif // MESSAGETEMPLATE_HPP_INCLUDED
